package com.tuo51.manager.controller;


import com.tuo51.evaluate.domain.appusers.Vip;
import com.tuo51.evaluate.repositories.appusers.VipRepository;
import com.tuo51.core.service.StaffService;
import com.tuo51.evaluate.service.appuser.ProductService;
import com.tuo51.evaluate.vo.VipVO;
import com.tuo51.foudation.exception.BusinessException;
import com.tuo51.foudation.model.ErrorCode;
import com.tuo51.foudation.model.MemVerificationCache;
import com.tuo51.foudation.model.StatusResponse;
import com.tuo51.foudation.utils.CopyUtils;
import com.tuo51.manager.service.DataAnalysisService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import static com.tuo51.foudation.constant.RequestConstant.TICKET;
import static org.springframework.web.bind.annotation.RequestMethod.GET;
import static org.springframework.web.bind.annotation.RequestMethod.POST;

@RestController
@RequestMapping("/mapi/product")
@Api(value = "/mapi/product", tags = "OP - 产品管理")
public class MAPIProductController {

    @Autowired
    DataAnalysisService analysisService;

    @Autowired
    StaffService staffService;

    @Autowired
    VipRepository vipRepository;

    @Autowired
    private ProductService productService;


    @Autowired
    MemVerificationCache cache;

    @RequestMapping(value = "/list", method = GET)
    @ApiOperation(value = "资源列表")
    public StatusResponse list(@RequestHeader(TICKET) String ticket,
                                   @RequestParam(value = "pageNum", required = false, defaultValue = "0")Integer pageNum,
                                   @RequestParam(value = "pageSize", required = false, defaultValue = "15")Integer pageSize ){

        cache.getStaffId(ticket);

        PageRequest pageRequest = PageRequest.of(pageNum, pageSize);

        Page<Vip> vipList = vipRepository.findAllByOrderByTypeAscPriceDescValueDesc(pageRequest);

        return StatusResponse.success(vipList);
    }

    @RequestMapping(value = "/info", method = GET)
    @ApiOperation(value = "内容详情")
    public StatusResponse info(@RequestHeader(TICKET) String ticket,
                                   Long productId){

        cache.getStaffId(ticket);

        Vip ret = vipRepository.getOne(productId);

        if (ret == null){
            throw new BusinessException(ErrorCode.MATERIAL_NOT_EXIST, "条目不存在");
        }

        return StatusResponse.success(ret);
    }

    @RequestMapping(value = "/edit", method = POST)
    @ApiOperation(value = "详情编辑, 有id时编辑，没id时创建")
    public StatusResponse edit(@RequestHeader(TICKET) String ticket,
                                    VipVO product){
        cache.getStaffId(ticket);

        Vip ret = null;
        if (product.getId() != null){
            ret = vipRepository.getOne(product.getId());
        }else{
            ret = new Vip();
        }

        if(StringUtils.isAnyEmpty(product.getName(), product.getCover(), product.getPromotion(), product.getCode())){
            throw new BusinessException(ErrorCode.PARAM_NOT_BLANK, "参数不能为空");
        }

        CopyUtils.copyProperties(product, ret);

        vipRepository.save(ret);

        return StatusResponse.success(ret);
    }

    @RequestMapping(value = "/delete", method = POST)
    @ApiOperation(value = "删除， not finished yet")
    public StatusResponse delete(@RequestHeader(TICKET) String ticket,
                                    String productId){
        cache.getStaffId(ticket);
        return StatusResponse.success();
    }
}

